/**
 * JAVACC DEMO 1.0
 * @copy right iussoft company All rights reserved.
 * @Package com.apache.uct.service.plugins  
 */
package com.apache.uct.service.plugins;

import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.apache.api.manager.BaseManager;
import com.apache.api.manager.ProxyManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.api.vo.ResultMsg;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SystemTools;
import com.apache.tools.ClassToolsUtil;
import com.apache.tools.DateUtils;
import com.apache.tools.StrUtil;
import com.apache.uct.common.entity.Act;
import com.apache.uct.common.entity.ActGive;
import com.apache.uct.common.entity.Dept;
import com.apache.uct.common.entity.Org;
import com.apache.uct.manager.ActGiveManager;
import com.apache.uct.manager.ActManager;
import com.apache.uct.manager.DeptManager;
import com.apache.uct.manager.OrgManager;

/**
 * description:  部门管理插件
 * @author dyhou 创建时间：2016-8-18  
 */
public class DeptActionPluginImpl extends AbstractSuperPlugin {

	private DeptManager deptManager;
	private ProxyManager uctProxyManager;
	private OrgManager orgManager;
	private ActManager actManager;
	private ActGiveManager actGiveManager;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo param) throws Exception {
		ResultEntity result = new ResultEntity();
		result.setResult("true");
		String method = String.valueOf(param.getParams("method"));
		if ("save".equals(method)) {
			result = save(result, param);
		} else if ("delete".equals(method)) {
			result = delete(result, param);
		} else if ("deptActData".equals(method)) {
			result = deptActData(result, param);
		}
		return result;
	}

	private ResultEntity save(ResultEntity result, ParamsVo param) {
		ParamsVo<Dept> vo = new ParamsVo<Dept>();
		Dept item = new Dept();
		item = (Dept) ClassToolsUtil.getInstance().convert2Bean(param.getParams(), item);
		long times = DateUtils.toLong(new Date());
		if (Validator.isNull(item.getFatherId()))
			item.setFatherId("0");
		if (Validator.isNotNull(item.getOrgId()) && Validator.isNull(item.getOrgEname())) {
			ParamsVo ovo = new ParamsVo();
			ovo.setInfoId(item.getOrgId());
			Org org = (Org) orgManager.getInfoById(ovo);
			item.setOrgEname(org.getOrgEname());
		}
		String userEname = String.valueOf(param.getParams("nowUserEname"));
		if (Validator.isNotNull(item.getDeptId())) {//对部门信息修改
			if (!"0".equals(item.getFatherId())) {
				ParamsVo pvo = new ParamsVo();
				pvo.setInfoId(item.getFatherId());
				Dept Dept = (Dept) deptManager.getInfoById(pvo);
				item.setFullEname(Dept.getFullEname() + "/" + item.getDeptEname());
				item.setFullCname(Dept.getDeptCname() + "/" + item.getDeptCname());
			} else {
				item.setFullEname("/" + item.getDeptEname());
				item.setFullCname("/" + item.getDeptCname());
			}
			item.setUpdateTime(times);
			int updateCount = item.getUpdateCount() == null ? 0 : item.getUpdateCount();
			item.setUpdateCount(updateCount + 1);
			item.setUpdateUser(userEname);
		} else {//添加部门信息
			if (Validator.isNull(item.getFatherId()))
				item.setFatherId("0");
			if (!"0".equals(item.getFatherId())) {
				ParamsVo pvo = new ParamsVo();
				pvo.setInfoId(item.getFatherId());
				Dept Dept = (Dept) deptManager.getInfoById(pvo);
				item.setFullEname(Dept.getFullEname() + "/" + item.getDeptEname());
				item.setFullCname(Dept.getDeptCname() + "/" + item.getDeptCname());
			} else {
				item.setFullEname("/" + item.getDeptEname());
				item.setFullCname("/" + item.getDeptCname());
			}
			item.setCreateUser(userEname);
//			item.setSysEname("uct");
			item.setSysEname(StrUtil.doNull(param.getParamsToStr("sysEname"),"uct"));
			item.setUpdateCount(0);
			item.setCreateTime(times);
			vo.setObj(item);
			vo.setKey("checkCodeName");
			String msg = String.valueOf(deptManager.execute(vo));
			if (Validator.isNotNull(msg)) {
				result.setMessage(msg);
				return result;
			}
		}
		vo.setObj(item);
		ResultMsg msg = super.save(vo, item.getDeptId());
		if ("T".equals(msg.getFlag())) {
			result.setEntity(msg.getFlag());
			result.setMessage(msg.getFlag());
		} else {
			result.setMessage(msg.getFlag());
		}
		return result;
	}

	private ResultEntity delete(ResultEntity result, ParamsVo param) {
		ParamsVo<Dept> vo = new ParamsVo<Dept>();
		String id = String.valueOf(param.getParams("deptId"));
		if (Validator.isNotNull(id)) {
			vo.setKey("deleteOrg");
			vo.setInfoId(id);
			vo.setParams("infoType", "dept");
			String msg = String.valueOf(uctProxyManager.doInvoke(vo));
			if (Validator.isNotNull(msg)) {
				result.setMessage("删除失败！");
				return result;
			}
		} else {
			result.setMessage("请选择要删除的部门！");
		}
		result.setEntity("T");
		result.setMessage("删除部门成功！");
		return result;
	}

	private ResultEntity deptActData(ResultEntity result, ParamsVo param) {
		String parentId = StrUtil.doNull(String.valueOf(param.getParams("id")), "0");
		String deptId = String.valueOf(param.getParams("deptId"));
		Act actVo = new Act();
		actVo.setFatherId(parentId);
		ParamsVo<Act> vo = new ParamsVo<Act>();
		vo.setObj(actVo);
		String nowOrgId = String.valueOf(param.getParams("nowOrgId"));
		String nowDeptId = String.valueOf(param.getParams("nowDeptId"));
		if (!Validator.isNull(nowOrgId) && "1".equals(SystemTools.getInstance().getValue("org_act_switch"))) {
			vo.setParams("userOrgId", nowOrgId);
			vo.setParams("sysUser", StrUtil.doNull(String.valueOf(param.getParams("nowSysUser")), "0"));//是否为系统用户
		}else if(Validator.isNotNull(nowDeptId) && "1".equals(SystemTools.getInstance().getValue("org_act_switch"))){
			vo.setParams("userDeptId", nowDeptId);
			vo.setParams("sysUser", StrUtil.doNull(String.valueOf(param.getParams("nowSysUser")), "0"));//是否为系统用户
		}
		vo.setKey("findActForOrg");
		List<Act> actList = (List<Act>) uctProxyManager.doInvoke(vo);
		ParamsVo<ActGive> agVo = new ParamsVo<ActGive>();
		agVo.setParams("roleId", deptId);
		agVo.setParams("giveType", "dept");
		agVo.setKey("GiveListByOrgId");
		List<ActGive> giveList = (List<ActGive>) actGiveManager.execute(agVo);

		JSONArray jary = new JSONArray();
		for (int i = 0; i < actList.size(); i++) {
			Act act = actList.get(i);
			JSONObject jvo = JSONObject.fromObject(act);
			if (act.getSubCount() != null && act.getSubCount() > 0) {
				jvo.put("state", "closed");
			} else {
				jvo.put("state", "opened");
			}
			String hasGive = hasGive(giveList, act.getActId()) ? "T" : "F";
			jvo.put("hasGive", hasGive);
			jary.add(jvo);
		}
		result.setEntity(jary);
		result.setMessage("查询成功");
		return result;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.uct.service.plugins.AbstractSuperPlugin#getBaseManager()  
	 */
	protected BaseManager getBaseManager() {
		return deptManager;
	}

	public void setDeptManager(DeptManager deptManager) {
		this.deptManager = deptManager;
	}

	public void setUctProxyManager(ProxyManager uctProxyManager) {
		this.uctProxyManager = uctProxyManager;
	}

	public void setOrgManager(OrgManager orgManager) {
		this.orgManager = orgManager;
	}

	public void setActManager(ActManager actManager) {
		this.actManager = actManager;
	}

	public void setActGiveManager(ActGiveManager actGiveManager) {
		this.actGiveManager = actGiveManager;
	}

}
